package com.sgu6.sgu6_machine.util;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class VideoUtil {
    protected static final String TAG = VideoUtil.class.getSimpleName();
    private static final String LOCAL_PATH = Environment.getExternalStorageDirectory().getPath();

    // 生成文件
    private static File makeFilePath(String filePath, String fileName) {
        File file = null;
        makeRootDirectory(filePath);
        try {
            file = new File(LOCAL_PATH + filePath + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            Log.i(TAG, "makeFilePath: " + e);
            e.printStackTrace();
        }
        return file;
    }

    // 生成文件夹
    private static void makeRootDirectory(String filePath) {
        File file;
        try {
            file = new File(LOCAL_PATH + filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (Exception e) {
            Log.i(TAG, "makeRootDirectory: " + e);
        }
    }

    public static List<String> read(String filePath, String fileName) {
        String msg;
        List<String> ads = new ArrayList<>();
        File file = makeFilePath(filePath, fileName);
        try {
            InputStream inStream = new FileInputStream(file);
            InputStreamReader inputReader = new InputStreamReader(inStream);
            BufferedReader bufferReader = new BufferedReader(inputReader);

            while ((msg = bufferReader.readLine()) != null) {
                System.out.println(msg);//打印从标准输入读取到的数据
                ads.add(msg);
            }
            inStream.close();
            inputReader.close();
            bufferReader.close();
        } catch (IOException e) {
            Log.i(TAG, "readFirstLine: " + e);
        }
        return ads;
    }

    public static void write(String filePath, String fileName, List<String> ads) {
        File file = makeFilePath(filePath, fileName);
        /**以读写的方式建立一个RandomAccessFile对象**/
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(file, false));
            for (int i = 0; i < ads.size(); i++) {
                out.write(ads.get(i));
                out.write(System.getProperty("line.separator"));
            }

            out.close();
        } catch (IOException e) {
        }
    }


    public static void appendLastLine(String filePath, String fileName, String line) {

        File file = makeFilePath(filePath, fileName);

        FileOutputStream outSTr;
        try {
            outSTr = new FileOutputStream(file, true);
            outSTr.write(line.getBytes());
            outSTr.write(Objects.requireNonNull(System.getProperty("line.separator")).getBytes());
            outSTr.flush();
            outSTr.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String readFirstLine(File file) {
        String firstLine;
        try {
            InputStream inStream = new FileInputStream(file);
            InputStreamReader inputReader = new InputStreamReader(inStream);
            BufferedReader bufferReader = new BufferedReader(inputReader);
            firstLine = bufferReader.readLine();
            if (firstLine == null) {
                firstLine = "累计碾米总数:" + 0;
            }
            inStream.close();
            inputReader.close();
            bufferReader.close();
            return firstLine;
        } catch (IOException e) {
            firstLine = "";
            Log.i(TAG, "readFirstLine: " + e);
        }
        return firstLine;
    }
}
